package com.li.controller.admin;

import com.li.DTO.HostDTO;
import com.li.VO.PageListVO;
import com.li.pojo.BuildingManage;
import com.li.pojo.Host;
import com.li.result.Result;
import com.li.service.HostService;
import com.li.util.ExcelPoi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * @Author: zhiqingLi
 * @Description: 业主管理
 * @Date: Created in 20:46 2024/3/13
 **/
@RestController
@Slf4j
@RequestMapping("/admin/host")
public class HostController {

    @Autowired
    private HostService hostService;

    //查询全部 加上模糊查询
    @PostMapping("/selShowHost")
    public Result<PageListVO> showList(@RequestBody HostDTO hostDTO){
        log.info("信息传递：{}",hostDTO);
        PageListVO pageListVO =  hostService.showList(hostDTO);
        return Result.success(pageListVO);
    }

    //添加
    @PostMapping("/add")
    public Result addSubregion(@RequestBody Host host){
        log.info("信息传递：{}",host);
        hostService.addSubregion(host);
        return Result.success();
    }

    //修改回显
    @GetMapping("/upd/{id}")
    public Result<Host> updGetById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        Host host = hostService.updGetById(id);
        return Result.success(host);
    }

    //修改数据
    @PostMapping("/updHost")
    public Result updSubregion(@RequestBody Host host){
        log.info("信息传递：{}",host);
        hostService.updSubregion(host);
        return Result.success();
    }

    //删除
    @GetMapping("/del/{id}")
    public Result delById(@PathVariable("id") Long id){
        log.info("信息传递：{}",id);
        hostService.delById(id);
        return Result.success();
    }

    //回显名字
    @GetMapping("/showNameByHostName/{name}")
    public Result<List<Host>> showNameByHostName(@PathVariable("name") String name){
        List<Host> hosts =  hostService.showNameByHostName(name);
        return Result.success(hosts);
    }

    //上传 excel 表格数据 // id 必然为4
    @PostMapping("/fileUpload")
    public Result<String> fileUpload(@RequestPart("file") MultipartFile file, @RequestParam("id")Integer id) throws Exception {
        InputStream inputStream = file.getInputStream();
        List<Host> read = ExcelPoi.read(inputStream, id);
        if (read.size()>0){
            for (Host host : read) {
                hostService.addSubregion(host);
            }
        }
        return Result.success();
    }
}
